/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distrbuted under the License is distrbuted on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.demo.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo.entity.Blog;
import com.example.demo.excel.BlogExcel;
import com.example.demo.excel.BlogImportListener;
import com.example.demo.service.IBlogService;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.apache.commons.codec.Charsets;
import org.springblade.common.cache.CacheNames;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author Chill
 */
@RestController
@RequestMapping("demo")
@AllArgsConstructor
@Api(value = "Demo", tags = "Demo")
public class BlogController extends BladeController implements CacheNames {

	private IBlogService blogService;

	/**
	 * 分页
	 */
	@GetMapping("/list")
	public R<IPage<Blog>> list( @RequestParam Map<String, Object> blog, Query query) {
		IPage<Blog> pages = blogService.page(Condition.getPage(query), Condition.getQueryWrapper(blog, Blog.class));
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	public R save(@RequestBody Blog blog) {
		return blogService.blogSave(blog);
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	public R update(@RequestBody Blog blog) {
		return blogService.blogUpdate(blog);
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	public R remove(@RequestParam String ids) {
		return blogService.blogRemove(ids);
	}

	/**
	 * 导入
	 */
	@PostMapping("/importBlog")
	public R importBlog(MultipartFile file) {
		String filename = file.getOriginalFilename();
		if (StringUtils.isEmpty(filename)) {
			throw new RuntimeException("请上传文件!");
		}
		if ((!StringUtils.endsWithIgnoreCase(filename, ".xls") && !StringUtils.endsWithIgnoreCase(filename, ".xlsx"))) {
			throw new RuntimeException("请上传正确的excel文件!");
		}
		InputStream inputStream;
		try {
			BlogImportListener importListener = new BlogImportListener(blogService);
			inputStream = new BufferedInputStream(file.getInputStream());
			ExcelReaderBuilder builder = EasyExcel.read(inputStream, BlogExcel.class, importListener);
			builder.doReadAll();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return R.success("操作成功");
	}

	/**
	 * 导出
	 */
	@GetMapping("/exportBlog")
	public void exportBlog(@RequestParam Map<String, Object> blog, HttpServletResponse response) throws IOException {
		QueryWrapper<Blog> queryWrapper = Condition.getQueryWrapper(blog, Blog.class);
		queryWrapper.lambda().eq(Blog::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<BlogExcel> list = blogService.exportBlog(queryWrapper);
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding(Charsets.UTF_8.name());
		String fileName = URLEncoder.encode("数据导出", Charsets.UTF_8.name());
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		EasyExcel.write(response.getOutputStream(), BlogExcel.class).sheet("blog数据表").doWrite(list);
	}

}
